package com.wnm.service.impl;

import java.util.UUID;

import org.apache.ibatis.session.SqlSession;

import com.wnm.dao.UserDao;
import com.wnm.pojo.User;
import com.wnm.service.UserService;
import com.wnm.util.DBUtils;
import com.wnm.util.MailUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
	@Autowired
	private UserDao userDao;

	@Override
	public boolean isExist(String userName) {
		User user = userDao.findByUserName(userName);
		if (user == null) {
			return false;
		} else {
			return true;
		}

	}

	@Override
	public void regist(User user) {
		String code = UUID.randomUUID().toString().replace("-", "");
		user.setCode(code);
		userDao.add(user);
		MailUtils.sendMail(user.getEmail(),
				"<h1>点击链接<a href='http://localhost:8080/wnm/user/active?code=" + code + "'>激活</a>商城账号</h1>", "商城账号激活");
	}

	@Override
	public User login(String username, String password) {
		User user = userDao.findUserByUsernameAndPassword(username, password);
		return user;
	}

	@Override
	public boolean active(String code) {
		User user=userDao.findUserByCode(code);
		if(user==null) {
			return false;
		}else {
			userDao.updateStatus(user);
			return true;
		}
	}

	@Override
	public void updatePasswordByUid(int uid,String newPassword) {
		userDao.updatePasswordByUid(uid,newPassword);
		
	}

}
